package com.achobeta.recruitment.controller.interact;

import com.achobeta.recruitment.common.AppResult;
import com.achobeta.recruitment.common.ResultCode;
import com.achobeta.recruitment.common.Rule;
import com.achobeta.recruitment.model.Feedback;
import com.achobeta.recruitment.model.Manager;
import com.achobeta.recruitment.model.WxUser;
import com.achobeta.recruitment.model.vo.FeedbackVO;
import com.achobeta.recruitment.service.interact.FeedBackService;
import com.achobeta.recruitment.utils.CheckUtils;
import com.achobeta.recruitment.utils.LimitUtils;
import com.achobeta.recruitment.utils.SessionUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 马拉圈
 * Date: 2023-10-31
 * Time: 23:22
 */
@Slf4j
@RestController
@RequestMapping("/feedback")
@Api(tags = "反馈相关")
public class FeedBackController {

    @Autowired
    private FeedBackService feedBackService;

    @ApiOperation(value = "提交反馈", notes = "无data")
    @PostMapping("/submit")
    public AppResult submit(HttpServletRequest request,
                            @NonNull @ApiParam("消息id") @RequestParam("messageId") Integer messageId,
                            @NonNull @ApiParam("反馈内容") @RequestParam("content") String content) {
        // 获取当前登录用户的消息
        WxUser wxUser = SessionUtils.getWxUser(request);
        String openid = wxUser.getOpenid();
//        String openid = guy;
        // 提交反馈
        feedBackService.submitFeedBack(messageId, openid, content);
        // 成功返回
        return AppResult.success();
    }


    @ApiOperation(value = "获取未读反馈数", notes = "data为未读私信数")
    @GetMapping("/count")
    public AppResult<Long> getNotAwareNumber(HttpServletRequest request) {
        // 获取当前登录用户
        Manager manager = SessionUtils.getManager(request);
        // 判断是否有权限
        if(!LimitUtils.hasPower(manager, Rule.NOT_AWARE_COUNT_RULE)) {
            log.warn(ResultCode.FAILED_MANAGER_NOT_POWERS.toString());
            return AppResult.failed(ResultCode.FAILED_MANAGER_NOT_POWERS);
        }
        Long count = feedBackService.getNotAwareNumber();
        return AppResult.success(count);
    }

    @ApiOperation(value = "获取反馈列表", notes = "data为反馈列表，包括对应被反馈的消息")
    @GetMapping("/list")
    public AppResult<List<FeedbackVO>> getFeedbackList(HttpServletRequest request) {
        // 获取当前登录用户
        Manager manager = SessionUtils.getManager(request);
        // 判断是否有权限
        if (!LimitUtils.hasPower(manager, Rule.FEEDBACK_LIST_RULE)) {
            log.warn(ResultCode.FAILED_MANAGER_NOT_POWERS.toString());
            return AppResult.failed(ResultCode.FAILED_MANAGER_NOT_POWERS);
        }
        List<FeedbackVO> feedbackVOS = feedBackService.getAllFeedback();
        return AppResult.success(feedbackVOS);
    }


    @ApiOperation(value = "标记反馈为已读", notes = "无data")
    @GetMapping("/read")
    public AppResult remarkRead(HttpServletRequest request,
                                @NonNull @ApiParam("反馈id") @RequestParam("id") Integer id) {
        // 获取当前登录用户
        Manager manager = SessionUtils.getManager(request);
        // 判断是否有权限
        if (!LimitUtils.hasPower(manager, Rule.READ_FEEDBACK_RULE)) {
            log.warn(ResultCode.FAILED_MANAGER_NOT_POWERS.toString());
            return AppResult.failed(ResultCode.FAILED_MANAGER_NOT_POWERS);
        }
        // 此反馈得存在
        Feedback feedback = feedBackService.getFeedbackById(id);
        CheckUtils.checkNULL(feedback);
        if(feedback.getDeleteState() == 1) {
            log.warn(ResultCode.FAILED_NOT_EXISTS.toString());
            return AppResult.failed(ResultCode.FAILED_NOT_EXISTS);
        }
        if(feedback.getState() == 1) {
            return AppResult.success();
        }
        // 调用service层
        feedBackService.remarkRead(id);
        // 成功
        return AppResult.success();
    }


}
